package com.hlm.demo.controller.business;

import com.hlm.demo.common.ResultWrapper;
import com.hlm.demo.entity.Account;
import com.hlm.demo.entity.BusinessInfo;
import com.hlm.demo.service.business.IBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/service")
public class businessController {

    @Autowired
    private IBusinessService businessService;


    // 查询 list.html 的所有数据
    @RequestMapping("/checkAll")
    @ResponseBody
    public Object checkAll(BusinessInfo businessInfo, String idCardNo, @RequestParam(defaultValue = "service_id") String order, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer count) {

        Integer serviceStatus = businessInfo.getServiceStatus();
        System.out.println(serviceStatus + " = serviceStatus");

        List<BusinessInfo> businessInfoList = businessService.businessCheck(businessInfo, idCardNo, order, page, count);
        System.out.println("size = " + businessInfoList.size());

        Integer total = businessService.businessTotalCountCheck(businessInfo, idCardNo);
        System.out.println("total" + total);


        return ResultWrapper.success(total, businessInfoList);

    }



    /**
     * 启用暂停开关
     * @return
     */
    @RequestMapping(value = "/serviceSwitch", method = RequestMethod.POST)
    public String serviceSwitch(String serviceId, Integer serviceStatus) {

        businessService.serviceSwitch(serviceId, serviceStatus);

        return "redirect:/service/service_list.html";
    }


    // 根据身份证查找账务账号
    @RequestMapping("/findAccount")
    @ResponseBody
    public Object findAccount(String idcardNo) {

        return businessService.findAccountIdByIdcardNo(idcardNo);

    }

    // 查询资费
    @RequestMapping("/costCheck")
    @ResponseBody
    public Object costCheck() {

        return ResultWrapper.success(businessService.costCheck());

    }


    // 添加业务
    @RequestMapping("/addBusiness")
    public String addBusiness(@Validated BusinessInfo businessInfo, BindingResult result, String passwordRepeat) {

        // 密码一致校验
        if (!passwordRepeat.equals(businessInfo.getLoginPasswd())){
            System.out.println("两次密码输入的不一致");
        } else{
            // 校验器校验
            if (result.hasErrors()){
                List<FieldError> errors = result.getFieldErrors();
                errors.forEach(System.out::println);
            }else {
                businessService.addBusiness(businessInfo);
            }

        }



        return "redirect:/service/service_list.html";

    }

    // 更改业务
    @RequestMapping("/modifyBusiness")
    public String modifyBusiness(BusinessInfo businessInfo) {

        Integer costId = businessInfo.getCostId();
        Integer serviceId = businessInfo.getServiceId();

        System.out.println("serviceId " + serviceId);
        System.out.println("costId " + costId);


        businessService.modifyBusiness(businessInfo);

        return "redirect:/service/service_list.html";
    }


    // 暂停时间记录器
    @RequestMapping("/pauseTime")
    public String pauseTime(String serviceId) {

        businessService.pauseTime(serviceId);

        return "redirect:/service/service_list.html";

    }

    // 暂停时间取消器
    @RequestMapping("/pauseTimeCancel")
    public String pauseTimeCancel(String serviceId) {

        businessService.pauseTimeCancel(serviceId);

        return "redirect:/service/service_list.html";

    }

    // 删除时间记录器
    @RequestMapping("/deleteTime")
    public String deleteTime(String serviceId) {

        businessService.deleteTime(serviceId);

        return "redirect:/service/service_list.html";
    }





}
